package com.yedean.factory.support;

import com.yedean.factory.BeanFactory;
import com.yedean.factory.config.BeanDefinition;

/**
 * @author yedean
 * @date 2022/6/28
 */

public abstract class AbstractBeanFactory extends DefaultSingletonRegistry implements BeanFactory {

    @Override
    public Object getBean(String name, Object... args) {
        return doGetBean(name,args);
    }

    @Override
    public Object getBean(String name) {
        return doGetBean(name,null);
    }
    
    public Object doGetBean(String name,Object[] args){
        //定义模板方法
        //1.先从单例缓存中取值
        Object bean = getSingleton(name);
        //2.判断单例取出的object是否为空
        if (bean == null) {
            BeanDefinition beanDefinition = getBeanDefinition(name);
            return createBean(name, beanDefinition,args);
        }
        return bean;
    }

    /**
     *  获取bean定义信息
     * @param name beanName
     * @return BeanDefinition
     */
    protected abstract BeanDefinition getBeanDefinition(String name);

    /**
     * 创建bean实例
     * @param name 名称
     * @param definition 定义信息
     * @param args 参数
     * @return bean实例
     */
    protected abstract Object createBean(String name, BeanDefinition definition,Object[] args);
}
